/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ModelJava;

import Model.Adresa;
import Model.HibernateUtil;
import Model.Uzivatel;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author papa
 */
public class UserManager {

    protected static Session session = HibernateUtil.getSessionFactory().openSession();

    public UserManager() {
    }

    public Uzivatel isNameValid(String jmeno){
        session.beginTransaction();
        Query q = session.createQuery("from Uzivatel u where u.email like '" + jmeno + "'");
        List l = q.list();
        session.getTransaction().commit();
        if(l.isEmpty())
            return null;
        else
        return (Uzivatel)l.get(0);
    }
    
    public boolean isPasswordValid(String jmeno, String heslo) {
        session.beginTransaction();
        Query q = session.createQuery("from Uzivatel u where u.email like '" + jmeno + "' and u.heslo like '" + SHA1hash.getSHA1(heslo) + "'");
        List l = q.list();
        session.getTransaction().commit();
        if(l.isEmpty()){
            return false;
        }else
        return true;
    }

    public static boolean isAllset(Uzivatel u){
        System.err.println(u.getJmeno() != null);
        System.err.println(u.getPrijmeni() != null);
        System.err.println(u.getAdresa() != null);
        System.err.println(u.getAdresa().getCp() != 0);
        System.err.println(u.getAdresa().getMesto() != null);
        System.err.println(u.getAdresa().getPsc() != 0);
        System.err.println(u.getAdresa().getStat() != null);
        System.err.println(u.getAdresa().getUlice() != null);
        System.err.println(u.getEmail() != null);
        System.err.println(u.getHeslo() != null);
        System.err.println(u.getTelefon() != 0);
        if(u.getJmeno() != null &&
                u.getPrijmeni() != null &&
                u.getAdresa() != null &&
                u.getAdresa().getCp() != 0 &&
                u.getAdresa().getMesto() != null &&
                u.getAdresa().getPsc() != 0 &&
                u.getAdresa().getStat() != null &&
                u.getAdresa().getUlice() != null &&
                u.getEmail() != null &&
                u.getHeslo() != null &&
                u.getTelefon() != 0){
            return true;
        }else return false;
    }

    public static void vlozData(Uzivatel u) {
        Transaction tr = null;
        try {
            tr = session.beginTransaction();
            //tady budu ukladat
            session.save(u.getAdresa());
            session.saveOrUpdate(u.getRole());
            session.saveOrUpdate(u);
            session.getTransaction().commit();
        } catch (HibernateException he) {
            System.out.println("HQL Chyba ve vlozData");
            he.printStackTrace();
            if (tr != null) tr.rollback();
            throw he;
        }finally{
//            session.close();
        }
    }
}
